#!/bin/bash

echo "============================================================"
echo "LIS系统 - 依赖安装脚本 (Linux/macOS)"
echo "============================================================"
echo

# 检查Python是否安装
if ! command -v python3 &> /dev/null; then
    echo "❌ 错误: 未找到Python3，请先安装Python 3.8或更高版本"
    echo "Ubuntu/Debian: sudo apt-get install python3 python3-pip"
    echo "CentOS/RHEL: sudo yum install python3 python3-pip"
    echo "macOS: brew install python3"
    exit 1
fi

echo "✅ Python已安装"
python3 --version

# 检查pip是否安装
if ! command -v pip3 &> /dev/null; then
    echo "❌ 错误: 未找到pip3，请先安装pip"
    echo "Ubuntu/Debian: sudo apt-get install python3-pip"
    echo "CentOS/RHEL: sudo yum install python3-pip"
    echo "macOS: brew install python3"
    exit 1
fi

echo "✅ pip已安装"
pip3 --version

echo
echo "🔄 正在升级pip..."
python3 -m pip install --upgrade pip

echo
echo "📦 选择安装模式:"
echo "1. 生产环境 (仅核心依赖)"
echo "2. 开发环境 (包含测试工具)"
echo "3. 完整安装 (所有依赖)"
echo

read -p "请选择 (1/2/3): " choice

case $choice in
    1)
        echo
        echo "🚀 安装生产环境依赖..."
        python3 -m pip install -r requirements_production.txt
        ;;
    2)
        echo
        echo "🚀 安装开发环境依赖..."
        python3 -m pip install -r requirements_dev.txt
        ;;
    3)
        echo
        echo "🚀 安装完整依赖..."
        python3 -m pip install -r requirements.txt
        ;;
    *)
        echo "❌ 无效选择"
        exit 1
        ;;
esac

if [ $? -ne 0 ]; then
    echo
    echo "❌ 依赖安装失败，请检查错误信息"
    exit 1
fi

echo
echo "✅ 依赖安装完成！"
echo
echo "📋 下一步:"
echo "1. 运行主程序: python3 scripts/main.py"
echo "2. 运行测试: python3 -m pytest test/"
echo "3. 查看文档: 阅读 README.md"
echo

if [ "$choice" = "2" ]; then
    echo "🛠️ 开发工具:"
    echo "- 代码格式化: black src/"
    echo "- 代码检查: flake8 src/"
    echo "- 类型检查: mypy src/"
    echo
fi

# 检查系统依赖 (Linux)
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    echo "🔍 检查系统依赖..."
    
    # 检查是否安装了必要的系统包
    if command -v apt-get &> /dev/null; then
        # Ubuntu/Debian
        echo "检测到Ubuntu/Debian系统"
        echo "如果需要，可以安装以下系统包:"
        echo "sudo apt-get install python3-dev build-essential"
    elif command -v yum &> /dev/null; then
        # CentOS/RHEL
        echo "检测到CentOS/RHEL系统"
        echo "如果需要，可以安装以下系统包:"
        echo "sudo yum install python3-devel gcc"
    fi
fi
